package com.company.oa.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.meeting.dto.MeetingFeedbackDTO;
import com.company.oa.meeting.entity.Meeting;
import com.company.oa.meeting.entity.MeetingFeedback;
import com.company.oa.meeting.mapper.MeetingFeedbackMapper;
import com.company.oa.meeting.mapper.MeetingMapper;
import com.company.oa.meeting.service.MeetingFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会议反馈服务实现类
 */
@Service
public class MeetingFeedbackServiceImpl extends ServiceImpl<MeetingFeedbackMapper, MeetingFeedback> implements MeetingFeedbackService {

    @Autowired
    private MeetingMapper meetingMapper;

    @Override
    @Transactional
    public boolean submitFeedback(MeetingFeedbackDTO feedbackDTO, Long userId, String userName) {
        // 检查会议是否存在
        Meeting meeting = meetingMapper.selectById(feedbackDTO.getMeetingId());
        if (meeting == null) {
            throw new RuntimeException("会议不存在");
        }
        
        // 检查是否已提交过反馈
        if (hasSubmittedFeedback(feedbackDTO.getMeetingId(), userId)) {
            throw new RuntimeException("您已提交过反馈");
        }
        
        // 创建反馈
        MeetingFeedback feedback = new MeetingFeedback();
        feedback.setMeetingId(feedbackDTO.getMeetingId());
        feedback.setUserId(userId);
        feedback.setUserName(userName);
        feedback.setRating(feedbackDTO.getRating());
        feedback.setComment(feedbackDTO.getComment());
        feedback.setCreateTime(LocalDateTime.now());
        
        return save(feedback);
    }

    @Override
    public IPage<MeetingFeedback> getFeedbackPage(int page, int size, Long meetingId) {
        Page<MeetingFeedback> pageParam = new Page<>(page, size);
        
        LambdaQueryWrapper<MeetingFeedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MeetingFeedback::getMeetingId, meetingId)
               .orderByDesc(MeetingFeedback::getCreateTime);
        
        return page(pageParam, wrapper);
    }

    @Override
    public Map<String, Object> getFeedbackStatistics(Long meetingId) {
        Map<String, Object> result = new HashMap<>();
        
        // 获取所有反馈
        LambdaQueryWrapper<MeetingFeedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MeetingFeedback::getMeetingId, meetingId);
        
        List<MeetingFeedback> feedbacks = list(wrapper);
        
        // 计算平均评分
        if (feedbacks.isEmpty()) {
            result.put("averageRating", 0);
            result.put("totalFeedbacks", 0);
            return result;
        }
        
        double totalRating = 0;
        for (MeetingFeedback feedback : feedbacks) {
            totalRating += feedback.getRating();
        }
        
        double averageRating = totalRating / feedbacks.size();
        
        result.put("averageRating", averageRating);
        result.put("totalFeedbacks", feedbacks.size());
        
        // 统计各评分人数
        Map<Integer, Integer> ratingCounts = new HashMap<>();
        for (int i = 1; i <= 5; i++) {
            ratingCounts.put(i, 0);
        }
        
        for (MeetingFeedback feedback : feedbacks) {
            int rating = feedback.getRating();
            ratingCounts.put(rating, ratingCounts.get(rating) + 1);
        }
        
        result.put("ratingCounts", ratingCounts);
        
        return result;
    }

    @Override
    public boolean hasSubmittedFeedback(Long meetingId, Long userId) {
        LambdaQueryWrapper<MeetingFeedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MeetingFeedback::getMeetingId, meetingId)
               .eq(MeetingFeedback::getUserId, userId);
        
        return count(wrapper) > 0;
    }
} 